package com.emagic.manage.common.drill;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;

import com.emagic.manage.events.ShareEvent;
import com.xitaiinfo.library.commons.rx.RxBus;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * <Pre>
 * 钻取工具类
 * </Pre>
 * <p>
 * Created by Administrator on 2016/5/10.
 */
public class DrillUtil {
    public static int index;
    private static final String TAG = DrillUtil.class.getSimpleName();

    public static final Map<String, DrillEntity> mDrillMap = new HashMap<>();


    static {
        mDrillMap.put(DrillType.DRILL_TYPE_2000, new DrillEntity(".common.ui.activity.AllHtmlActivity", ".common.ui.activity.AllHtmlActivity"));
        mDrillMap.put(DrillType.DRILL_TYPE_2001, new DrillEntity(".modules.repairmodule.activity.RepairListActivity", ".modules.repairmodule.activity.RepairDetailActivity"));
        mDrillMap.put(DrillType.DRILL_TYPE_2002, new DrillEntity(".modules.complaintmodule.activity.ComplainListActivity", ".modules.complaintmodule.activity.ComplainDetailActivity"));
        mDrillMap.put(DrillType.DRILL_TYPE_2005, new DrillEntity(".modules.qualitymodule.activity.QualityMainActivity", ""));
        mDrillMap.put(DrillType.DRILL_TYPE_200501, new DrillEntity(".modules.qualitymodule.activity.OrderListActivity", ".modules.qualitymodule.activity.OrderDetailActivity"));
        mDrillMap.put(DrillType.DRILL_TYPE_2006, new DrillEntity(".modules.visitormodule.activity.VisitorRecordListActivity", ""));
        mDrillMap.put(DrillType.DRILL_TYPE_2007, new DrillEntity(".modules.equipmentmodule.activity.EquipmentMainActivity", ""));
        mDrillMap.put(DrillType.DRILL_TYPE_2008, new DrillEntity(".modules.keepwatchmodule.activity.KeepWatchMainActivity", ""));
        mDrillMap.put(DrillType.DRILL_TYPE_2009, new DrillEntity(".modules.noticemodule.activity.NoticeActivity", ".modules.noticemodule.activity.NoticeDetailActivity"));
        mDrillMap.put(DrillType.DRILL_TYPE_2010, new DrillEntity(".modules.examinationmodule.activity.WorkAttendanceActivity", ""));
        mDrillMap.put(DrillType.DRILL_TYPE_2011, new DrillEntity(".modules.examinationmodule.activity.ExaminationApprovalActivity", ".modules.examinationmodule.activity.ApprovalDetailActivity"));
        mDrillMap.put(DrillType.DRILL_TYPE_2013, new DrillEntity(".modules.organizationmodule.activity.OrganizationActivity", ""));
        mDrillMap.put(DrillType.DRILL_TYPE_1025, new DrillEntity("", ""));
    }


    public static void handleDrill(Context ctx, Object data) throws Exception {

        JSONObject jsonObject;
        String method = null, kind = null, id = null, ispending = null;
        try {
            jsonObject = new JSONObject(String.valueOf(data));
            if (jsonObject.has("method")) {
                method = jsonObject.getString("method");
            }

            if (jsonObject.has("kind")) {
                kind = jsonObject.getString("kind");
            }

            if (jsonObject.has("id")) {
                id = jsonObject.getString("id");
            }
            if (jsonObject.has("ispending")) {
                ispending = jsonObject.getString("ispending");
            }
        } catch (JSONException e) {
            throw e;
        }

        if (TextUtils.isEmpty(method) || TextUtils.isEmpty(kind)) {
            throw new Exception("method or kind cannot be empty.");
        }

        handleDrill(ctx, method, kind, id, ispending);

    }

    /**
     * @param ctx
     * @param method 模块的code
     * @param kind   0:列表     1:明细
     * @param id     标示rid
     * @throws Exception
     */
    public static void handleDrill(Context ctx, String method, String kind, String id, String ispending) throws Exception {
        if (mDrillMap.containsKey(method)) {


            DrillEntity drillEntity = mDrillMap.get(method);

            if (DrillType.DRILL_TYPE_1025.equals(method)) {//分享的特殊处理
                RxBus.getDefault().post(new ShareEvent(id));
                return;
            }

            if (kind.equals(DrillEntity.KIND_LIST)) {//表示钻取list页面

                startActivity(ctx, drillEntity.getListClzName(), id);

            } else if (kind.equals(DrillEntity.KIND_DETAIL)) {//表示钻取detail页面

                startActivity(ctx, drillEntity.getDetailClzName(), id);

            }
        }
    }

    private static void startActivity(Context ctx, String className, String id) throws Exception {
        try {
            Class clz = Class.forName("com.emagic.manage" + className);

            //判断clz 是否Drillable子类
            if (Drillable.class.isAssignableFrom(clz)) {
                Drillable drillable = (Drillable) clz.newInstance();
                Intent i = drillable.makeDrillIntent(id);
                i.setClass(ctx, clz);
                ctx.startActivity(i);
            }

        } catch (ClassNotFoundException e) {
            Log.e(TAG, "class not found, please check your code.", e);
            throw e;
        } catch (InstantiationException e) {
            Log.e(TAG, e.getMessage(), e);
            throw e;
        } catch (IllegalAccessException e) {
            Log.e(TAG, e.getMessage(), e);
            throw e;
        }
    }
}
